<template>
	<view class="pages-user-login" v-if="isLoad">

		<view class="page-height flex-center flex-column">
			<image mode="aspectFill" lazy-load class="logo-img mb-md" :src="base_info.app_logo"></image>
			<view class="f-caption c-caption">{{base_info.app_text}}</view>
			<view @tap="$refs.show_rule_item.open()" class="wechat-login flex-center f-title c-base" :style="{
			background: `linear-gradient(90deg, ${primaryColor} 0%, ${subColor} 99%)`}"><i
					class="iconfont iconweixin mr-sm"></i>微信登录
			</view>
		</view>

		<uni-popup ref="show_rule_item" type="center" :maskClick="false">
			<view class="common-popup-content fill-base pd-lg radius-34">
				<view class="title">温馨提示</view>
				<view class="f-desc c-title mt-lg">
					登录即表示您已详细阅读并同意<span @tap.stop="goDetail(1)" :style="{color:primaryColor}">
						《用户隐私协议》
					</span>与<span @tap.stop="goDetail(2)" :style="{color:primaryColor}">
						《个人信息保护指引》
					</span>
				</view>
				<view class="button">
					<view @tap.stop="$refs.show_rule_item.close()" class="item-child">取消</view>
					<view @tap.stop="wxLogin" class="item-child c-base"
						:style="{background: primaryColor,color:'#fff'}">确定</view>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from 'vuex';
	import siteInfo from '@/siteinfo.js';
	export default {
		components: {},
		data() {
			return {
				isLoad: false,
				base_info: {}
			}
		},
		async onLoad() {
			console.log('pages/login')
			let {
				autograph = '',
					appLogin = ''
			} = this
			console.log('autograph,appLogin', autograph, appLogin)
			if (autograph && appLogin) {
				this.toLogin(appLogin)
				return
			}
			let data = await this.$api.base.getConfig()
			this.base_info = data
			if (!data.primaryColor) {
				data.primaryColor = '#A40035'
			}
			if (!data.subColor) {
				data.subColor = '#F1C06B'
			}
			let configInfo = Object.assign(this.$util.deepCopy(this.configInfo), data)
			this.updateConfigItem({
				key: 'configInfo',
				val: configInfo
			})
			this.isLoad = true
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			autograph: state => state.user.autograph,
			appLogin: state => state.user.appLogin,
			userInfo: state => state.user.userInfo,
		}),
		methods: {
			...mapActions(['getConfigInfo', 'getUserInfo']),
			...mapMutations(['updateConfigItem', 'updateUserItem']),
			//微信登录
			async wxLogin() {
				this.$refs.show_rule_item.close()
				try {
					let [providerErr, providerData] = await uni.getProvider({
						service: 'oauth',
					});
					let [loginErr, loginData] = await uni.login({
						provider: 'weixin'
					});
					let [infoErr, infoData] = await uni.getUserInfo({
						provider: 'weixin'
					})

					let {
						userInfo = {}
					} = infoData
					let {
						openId = ''
					} = userInfo
					if (!openId) return
					this.$util.showLoading({
						title: "登录中..."
					})
					try {
						this.toLogin(userInfo)
					} catch (e) {
						this.$util.hideAll()
					}
				} catch (e) {
					this.$util.showToast({
						title: '请先安装微信或升级版本'
					});
				}

			},
			async toLogin(userInfo) {
				let user_info = await this.$api.base.appLogin({
					data: userInfo
				})
				this.updateUserItem({
					key: 'appLogin',
					val: userInfo
				})
				let {
					autograph,
					data
				} = user_info
				this.$util.hideAll()
				this.updateUserItem({
					key: 'userInfo',
					val: data
				})
				this.updateUserItem({
					key: 'autograph',
					val: autograph
				})
				if (!this.configInfo.id) {
					await this.getConfigInfo()
				}
				let {
					phone = ''
				} = data
				phone = 1
				this.$util.goUrl({
					url: phone ? `/pages/service` : `/user/pages/phone`,
					openType: `reLaunch`
				})
			},
			goDetail(type) {
				let {
					siteroot
				} = siteInfo
				let href = siteroot.split('index.php')[0]
				let page = type == 1 ? 'protocol' : 'information'
				let url = `${href}${page}.html`
				this.$util.goUrl({
					url,
					openType: 'web'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.pages-user-login {

		.page-height {
			height: 100vh;
		}

		.logo-img {
			width: 160rpx;
			height: 160rpx;
		}

		.wechat-login {
			width: 522rpx;
			height: 98rpx;
			box-shadow: 0 18rpx 9rpx 0 rgba(0, 188, 82, 0.07);
			border-radius: 49rpx;
			margin: 200rpx 0;
		}

	}
</style>
